package com.lemon.play.goai.posjudge;

import java.util.List;

import android.graphics.Point;
import android.util.Log;

import com.lemon.play.goai.posjudge.util.ArrayTools;

public class Desk implements Cloneable {

	public static int SIZE = 19;

	public Desk() {
		for (int i = 0; i < 19; i++) {
			for (int j = 0; j < 19; j++) {
				spots[i][j] = new Spot(i, j, null);
			}
		}
	}

	// For lemon game
	public void setDeskFromGtp(List<Point> _blackStones, List<Point> _whiteStones) {
		for (int i = 0; i < 19; i++) {
			for (int j = 0; j < 19; j++) {
				setSpot(i, j, Spot.NONE);
			}
		}

		for (int i = 0; i < _blackStones.size(); i++) {
			Log.d("Desk", "black line:" + _blackStones.get(i).y + " col:" + _blackStones.get(i).x);
		}

		for (int i = 0; i < _whiteStones.size(); i++) {
			Log.d("Desk", "white line:" + _whiteStones.get(i).y + " col:" + _whiteStones.get(i).x);
		}

		for (int i = 0; i < _blackStones.size(); i++) {
			setSpot(_blackStones.get(i).y, _blackStones.get(i).x, Spot.BLACK);
		}

		for (int i = 0; i < _whiteStones.size(); i++) {
			setSpot(_whiteStones.get(i).y, _whiteStones.get(i).x, Spot.WHITE);
		}
	}

	public void setSpot(Spot spot) {
		spots[spot.getX()][spot.getY()] = spot;
	}

	public void setSpot(int x, int y, Boolean camp, int order) {
		this.setSpot(new Spot(x, y, camp, order));
	}

	public void setSpot(int x, int y, Boolean camp) {
		this.setSpot(x, y, camp, Spot.PREPARED);
	}

	public void setEat(Spot spot) {
		this.setSpot(new Spot(spot.getX(), spot.getY(), Spot.NONE, Spot.PREPARED));
	}

	public void removeSpot(Spot spot) {
		this.removeSpot(spot.getX(), spot.getY());
	}

	public void removeSpot(int x, int y) {
		spots[x][y] = new Spot(x, y, Spot.NONE, Spot.PREPARED);
	}

	public void setEat(int x, int y) {
		this.setSpot(new Spot(x, y, Spot.NONE, Spot.PREPARED));
	}

	public Spot getSpot(int x, int y) {
		return spots[x][y];
	}

	public Spot getSpot(Point location) {
		return spots[location.x][location.y];
	}

	public void setDesk(Spot[][] spots) {
		if (spots.length != 19)
			throw new IllegalArgumentException();
		for (Spot[] line : spots) {
			if (line.length != 19) {
				throw new IllegalArgumentException();
			}
		}
		this.spots = spots;
	}

	public Spot[][] getDesk() {
		return spots;
	}

	@Override
	public Object clone() {
		Desk desk = new Desk();
		desk.spots = ArrayTools.<Spot[][]> deepClone(this.spots);
		return desk;
	}

	private Spot[][] spots = new Spot[19][19];

}
